package ddd2bank;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Logic {
    public void runBank(){
        Scanner sc = new Scanner(System.in);
        LocalDate Date = LocalDate.of(2022,1,1);
        ArrayList<Deposit> deposits = new ArrayList<>();

        while(true) {
            System.out.println("当前时间：" + Date);
            System.out.println("save 存钱/take 取钱/date 调日期/exit 退出");
            System.out.println("save 存入金额(数字) 存入天数(数字)----date 增加的天数(数字)----take");
            String input = sc.nextLine();
            if (input.equals("exit")) {
                break;
            }
            if (input.equals("take")) {
                take(deposits,Date);
            }
            if (input.matches("^save\\s+(\\d+)\\s+(\\d+)$")) {
                // 提取两个数字部分
                Pattern pattern = Pattern.compile("^save\\s+(\\d+)\\s+(\\d+)$");
                Matcher matcher = pattern.matcher(input);
                if (matcher.find()) {
                    int firstNumber = Integer.parseInt(matcher.group(1));
                    int secondNumber = Integer.parseInt(matcher.group(2));
                    deposits.add(save(firstNumber, secondNumber,Date));
                }
            }

            if (input.matches("^date\\s+(-?\\d+)$")) {
                Pattern pattern = Pattern.compile("^date\\s+(-?\\d+)$");
                Matcher matcher = pattern.matcher(input);
                if (matcher.find()) {
                    int days = Integer.parseInt(matcher.group(1));
                    Date = Date.plusDays(days);
                }
            }
        }
    }








    public Deposit save(double depositAmount,int depositTerm,LocalDate Date){
        Deposit deposit = new Deposit();
        deposit.take(depositAmount);
        deposit.setDepositTerm(depositTerm);
        deposit.setSaveDate(Date);
        System.out.println("存入"+ deposit.getDepositAmount() +"元成功！利率为"+ getRace(deposit.getDepositTerm()));

        return deposit;
    }
    public void take(ArrayList<Deposit> deposits, LocalDate currentDate){
        for (Deposit deposit : deposits) {
            long days = ChronoUnit.DAYS.between(deposit.getSaveDate(), currentDate);
            if (days >= deposit.getDepositTerm()) {
                double interest = deposit.getDepositAmount() *
                        getRace(deposit.getDepositTerm()) *
                        ((double) deposit.getDepositTerm() / 365);
                deposit.setWithDrawableAmount(interest + deposit.getDepositAmount());
                deposit.setStatus("已到期");
            } else {
                deposit.setStatus("未到期");
            }
        }
        int count = 0;
        System.out.println("------------");
        for (Deposit deposit : deposits) {
            System.out.println((count+1)+": | 存入：" + deposit.getDepositAmount()
                    + " | 存期：" + deposit.getDepositTerm()
                    + "天 | " + deposit.getStatus()
                    + " | 可取：" + deposit.getWithDrawableAmount() + " |");
            count++;
        }
        System.out.println("------------");
        System.out.println("选择你要取出的目标：（请输入数字）,按下0退出");
        Scanner sc = new Scanner(System.in);
        String choice = sc.nextLine();
        if (choice.equals("0")) {
            return;
        }
        if (choice.matches("\\d+")) {
            if (Integer.parseInt(choice) <= count) {
                deposits.remove(Integer.parseInt(choice)-1);
                System.out.println("取出成功！");
            }else {
                System.out.println("没有该目标");
            }
        }else {
            System.out.println("请输入数字！！！");
        }

    }


     public double getRace(int day){
        if(day < 30){
            return 0.05;
        }
        else if(day < 60){
            return 0.1;
        }
        else if(day < 90){
            return 0.15;
        }
        else if(day < 180){
            return 0.20;
        }
        else if(day < 365){
            return 0.25;
        }
        else {
            return 0.30;
        }
     }
}
